package com.device.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.device.Constant.BaseConstant;
import com.device.Entity.Device;
import com.device.Entity.Dto.SessionWebUserDto;
import com.device.Entity.Lab;
import com.device.Exception.BaseException;
import com.device.Service.DeviceService;
import com.device.Service.LabService;
import com.device.annotation.GlobalInterceptor;
import com.device.common.PageResult;
import com.device.common.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lab")
public class LabController {
    @Resource
    private LabService labService;
    @Resource
    private DeviceService deviceService;

    @RequestMapping("/add")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result add(Lab lab) {
        labService.add(lab);
        return Result.success();

    }

    @RequestMapping("/list")
    @GlobalInterceptor(checkParam = true, checkLogin = true)
    public Result list(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = labService.getpage(pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }

    @RequestMapping("/ulist")
    @GlobalInterceptor(checkParam = true, checkLogin = true)
    public Result ulist(HttpSession httpSession, Integer pageNo, Integer pageSize, String keyword) {
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        Integer uid=sessionWebUserDto.getId();
        PageResult pageResult = labService.getpage1(uid,pageNo, pageSize, keyword);
        return Result.success(pageResult);
    }
    @RequestMapping("/all")
    @GlobalInterceptor(checkParam = true, checkLogin = true)
    public Result all() {
List<Map<String,Object>> lists=new ArrayList<>();

      List<Lab> list=labService.list();
      for (Lab lab:list){
          Map<String,Object> map=new HashMap<>();
          map.put("id",lab.getId());
          map.put("name",lab.getName());
          lists.add(map);
      }
        return Result.success(lists);
    }


    @RequestMapping("/edit")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result edit(Lab lab) {
        labService.edit(lab);
        return Result.success();

    }

    @RequestMapping("/delete")
    @GlobalInterceptor(checkLogin = true, checkParam = true)
    public Result delete(Integer id) {

        Lab lab = labService.getById(id);
        if (lab == null) {
            throw new BaseException("该实验室不存在");
        }
        LambdaQueryWrapper<Device> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Device::getSysid,id);
        Integer count= Math.toIntExact(deviceService.count(lambdaQueryWrapper));
        if(count>0){
            throw new BaseException("当前实验室下有设备，不能删除");
        }
        labService.removeById(id);
        return Result.success();
    }

}
